package hot;

/**
 * @author way
 * @create 2025-03-19 20:24
 */
public class _76_minWindow {
    public String minWindow(String s, String t) {
        int[] map = new int[128];
        for (int i = 0; i < t.length(); i++) {
            map[t.charAt(i)]++;
        }

        int windowsLength = s.length() + 1;
        int left = 0;
        int right = 0;
        int count = t.length();
        int start = 0;
        while (right < s.length()) {
            char c = s.charAt(right);
            if (map[c] > 0) {
                count--;
            }
            map[c]--;
            while (count == 0) {
                if (right - left + 1 < windowsLength) {
                    windowsLength = right - left + 1;
                    start = left;
                }
                if (map[s.charAt(left)] == 0) count++;
                map[s.charAt(left)]++;
                left++;
            }
            right++;
        }
        return windowsLength == s.length() + 1 ? "" : s.substring(start, start + windowsLength);
    }
}
